package kin.system.util;



import kin.context.system.RequestContext;


import org.springframework.beans.BeansException;  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;  
import org.springframework.context.ApplicationContextAware;  

import org.springframework.web.context.support.WebApplicationContextUtils;
/** 
 * 
 * @author MinFei 
 */  

public class ApplicationContextHelper implements ApplicationContextAware {  
    private static ApplicationContext appCtx;  
    /** 
     * 此方法可以把ApplicationContext对象inject到当前类中作为一个静态成员变量。 
     * @param applicationContext ApplicationContext 对象. 
     * @throws BeansException 
     */  
    @Autowired
    public void setApplicationContext( ApplicationContext applicationContext ) throws BeansException {  
        appCtx = applicationContext;  
    }  
    /** 
     * 这是一个便利的方法，帮助我们快速得到一个BEAN 
     * @param beanName bean的名字 
     * @return 返回一个bean对象 
     * @throws Exception 
     */  
    public static Object getBean( String beanName)  {  
    	
    	
    	if(appCtx==null){
    		if(RequestContext.getRequest()!=null)
    		
    		appCtx = WebApplicationContextUtils.getWebApplicationContext(RequestContext.getRequest().getSession().getServletContext());
    		
    	}
    	
    	//System.out.println("count:"+appCtx.getBeanDefinitionCount());
    	return appCtx.getBean(beanName); 
    }  
    
    public static ApplicationContext getApplicationContext(){
    	return appCtx;
    }
    
  

    
}  
